//
//  FXTabBarController.swift
//  微博(Swift)
//
//  Created by V on 15/12/26.
//  Copyright © 2015年 V. All rights reserved.
//

import UIKit

class FXTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 1. 自定义Bar
        let tabBar = FXTabBar()
        setValue(tabBar, forKeyPath: "tabBar")
        // 2. 添加自控制器
        addChildViewControllers()
        // 点击撰写按钮的闭包实现
        tabBar.composeButtonClickClourse = {[weak self] in
            // 此闭包使用 self 会造成循环引用。原因：tabBar－》self－》闭包－》self
            print("点击了撰写")
            let composeView = FXCompose()
//            let window = UIApplication.sharedApplication().keyWindow
//            window?.addSubview(composeView)
            if self != nil{
            composeView.show(self!)
            }
        }
    }
    
    
    
    // 添加 4 个控制器
     func addChildViewControllers() {
        addChildViewController(FXHomeController(), imageName: "tabbar_home", title: "首页")
        addChildViewController(FXMessageController(), imageName: "tabbar_message_center", title: "消息")
        addChildViewController(FXDiscoverController(), imageName: "tabbar_discover", title: "发现")
        addChildViewController(FXProfileController(), imageName: "tabbar_profile", title: "我")
    }
    
    
    
    // 添加子控制器
    func addChildViewController(childController: UIViewController, imageName: String, title: String) {
        childController.tabBarItem.image = UIImage(named: imageName)?.imageWithRenderingMode(.AlwaysOriginal)
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        /** 
            带文字的图片 直接添加到 tabBar.image 上边会出现 位置偏上的问题，通过设置偏移量来解决这个问题
            childController.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0)
        */
        // 设置 文字
//        childController.tabBarItem.title = title
//        childController.navigationItem.title = title
        // 上边的两句话 等同于这一句话：childController.title = title
        childController.title = title
        
        // 改变 tabBar 中的字体颜色 方法1:
        let dict = [NSForegroundColorAttributeName: UIColor.orangeColor()]
        childController.tabBarItem.setTitleTextAttributes(dict, forState: UIControlState.Selected)
        addChildViewController(FXNavigationController(rootViewController: childController))
        // 改变 tabBar 中的字体颜色 方法2:
        // UITabBar.appearance().tintColor = UIColor.cyanColor()
    }
    
    
    
    

    

    
    
    
}
